import org.springframework.jdbc.core.JdbcTemplate;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WorkTimeChecker {
    private static final Logger log = Logger.getLogger(WorkTimeChecker.class.getName());
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");

    public int checkWorkTime(JdbcTemplate jt, String workgroup_id) {
        int remind;
        try {
            LocalDate today = LocalDate.now();
            log.log(Level.INFO, "系统当前日期: {0}", today.format(DATE_FORMAT));

            LocalTime nowTime = LocalTime.now();
            log.log(Level.INFO, "系统当前时间: {0}", nowTime.format(TIME_FORMAT));

            log.log(Level.INFO, "工作组ID: {0}", workgroup_id);

            List<Map<String, Object>> oldDateList = queryHoliday(jt, workgroup_id, today.minusDays(1));
            log.log(Level.INFO, "昨天-节假日查询结果1: {0}", oldDateList.toString());

            List<Map<String, Object>> nowDateList = queryHoliday(jt, workgroup_id, today);
            log.log(Level.INFO, "当天-节假日查询结果: {0}", nowDateList.toString());

            boolean isWorkTime = checkIsWorkTime(jt, oldDateList, nowDateList, nowTime);
            log.log(Level.INFO, "当前时间是否非工作时间检查结果: {0}", isWorkTime);

            if (!isWorkTime) {
                log.log(Level.INFO, "当前为非工作时间");
                remind = 0;
            } else {
                log.log(Level.INFO, "当前不是非工作时间，返回-1检查工作组工作时间");
                remind = -1;
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage(), e);
            remind = -1;
        }
        log.log(Level.INFO, "是否非工作时间检查结果: {0}", remind);
        return remind;
    }

    private List<Map<String, Object>> queryHoliday(JdbcTemplate jt, String workgroup_id, LocalDate date) {
        String sql = "Select Date_Format(Freedate, '%Y-%m-%d') worktimeflag From c_Freedate_Chat c Where ? = Date_Format(Freedate, '%Y-%m-%d') And (c.Workgroup_Id = ? Or c.Workgroup_Id = '') Limit 1";
        return jt.queryForList(sql, date.toString(), workgroup_id);
    }

    private boolean checkIsWorkTime(JdbcTemplate jt, List<Map<String, Object>> oldDateList, List<Map<String, Object>> nowDateList, LocalTime nowTime) {
        boolean isWorkTime = true;
        try {
            String timeType = getTimeType(oldDateList, nowDateList);
            log.log(Level.INFO, "时间类型识别 识别类型: {0}", timeType);

            String timeConf = KHDBConfig.getParamConfig(jt, "ChatWork_Time_" + timeType);
            timeConf = KHUtil.isNull(timeConf) ? "09:00:00~18:00:00" : timeConf;
            log.log(Level.INFO, "时间类型识别 类型时间配置: {0}", timeConf);

            String[] timeArr = timeConf.split(";");
            for (String timePartStr : timeArr) {
                String[] timePart = timePartStr.split("~");
                LocalTime startTime = LocalTime.parse(timePart[0], TIME_FORMAT);
                LocalTime endTime = LocalTime.parse(timePart[1], TIME_FORMAT);

                isWorkTime = belongCalendar(nowTime, startTime, endTime);

                if (isWorkTime) {
                    break;
                }
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, "时间类型识别 异常", e);
        }
        return isWorkTime;
    }

    private String getTimeType(List<Map<String, Object>> oldDateList, List<Map<String, Object>> nowDateList) {
        if (oldDateList.size() <= 0 && nowDateList.size() > 0) {
            return "1";
        } else if (oldDateList.size() > 0 && nowDateList.size() > 0) {
            return "2";
        } else if (oldDateList.size() > 0 && nowDateList.size() <= 0) {
            return "3";
        } else {
            return "4";
        }
    }

    private boolean belongCalendar(LocalTime nowTime, LocalTime startTime, LocalTime endTime) {
        return!nowTime.isBefore(startTime) &&!nowTime.isAfter(endTime);
    }

    // 以下是模拟的外部类和方法，实际使用时需要替换为真实的实现
    static class KHDBConfig {
        public static String getParamConfig(JdbcTemplate jt, String key) {
            return null;
        }
    }

    static class KHUtil {
        public static boolean isNull(String str) {
            return str == null || str.isEmpty();
        }
    }

    public static void main(String[] args) {
        // 这里需要替换为实际的 JdbcTemplate 实例
        JdbcTemplate jt = null;
        WorkTimeChecker checker = new WorkTimeChecker();
        checker.checkWorkTime(jt, "1");
    }
}